*******************************************************************************
* 2. Clean the household/proxy interview data for wave 2

use "$data_input/OWWA_Proxy HH_Wave2", clear

* First cleaning
missings dropvars, force
compress

***********************************************************
* Prepare financial outcomes

* Generate savings variables
gen hsw_savings_2 = 0 if hh_d16_set_aside_savings_2==4 | hh_d16_set_aside_savings_2==2 | hh_d17_savings_hsw_2==1
* use continuous dollar-variable 
replace hsw_savings_2=hh_d17_savings_hsw_con_2 if hh_d17_savings_hsw_con_2!=.c
recode hsw_savings_2 (.=.c)

gen fam_savings_2 = 0 if hh_d16_set_aside_savings_2==4 | hh_d16_set_aside_savings_2==1 | hh_d18_savings_family_2==1
* use continuous dollar-variable 
replace fam_savings_2=hh_d18_savings_family_con_2 if hh_d18_savings_family_con_2!=.c
recode fam_savings_2 (.=.c)

gen total_savings_2 = fam_savings_2 + hsw_savings_2
gen share_hsw_sav_2 = hsw_savings_2/total_savings_2
gen any_fam_savings_2 = fam_savings_2 > 0 & fam_savings_2 < .
gen any_hsw_savings_2 = hsw_savings_2 > 0 & hsw_savings_2 < .

gen l_total_sav_2 = log(total_savings_2+1)
gen l_hsw_savings_2 = log(hsw_savings_2+1)
gen l_fam_savings_2 = log(fam_savings_2+1)

lab var total_savings_2 "Total amount of savings (in PHP)"
lab var share_hsw_sav_2 "Share of HSW savings"
label var hsw_savings_2 "HSW savings (PHP)"
label var fam_savings_2 "Family savings (PHP)"
label var l_total_sav_2 "Total savings (log)"
label var l_hsw_savings_2 "HSW savings (log)"
label var l_fam_savings_2 "Family savings (log)"

* use continuous dollar-variable 
gen amount_remit_2 = hh_d11_amount_send_money_con_2

rename hh_d10_no_send_money_times_2 freq_remit_2

gen remit_total_2 = amount_remit_2 * freq_remit_2

gen l_am_remit_2 = log(amount_remit_2+1)
gen l_remit_total_2 = log(remit_total_2+1)

lab var amount_remit_2 "Remittances most recent transaction (PHP)"
lab var l_am_remit_2 "Remittances most recent transaction (log)"
lab var freq_remit_2  "Frequency of remittances in past six months"
label var remit_total_2 "Remittances in past six months (USD)"
label var l_remit_total_2 "Remittances in past six months (log)"

* Prepare communication intensity
sum hh_b2_text_family_2 hh_b3_talk_family_times_2 hh_b4_length_conversation_mins_2, detail

* Recode Frequency of text messaging to days per week
gen text_freq_2=7 if hh_b2_text_family_2==1
replace text_freq_2=5 if hh_b2_text_family_2==2
replace text_freq_2=2.5 if hh_b2_text_family_2==3
replace text_freq_2=1 if hh_b2_text_family_2==4
replace text_freq_2=0.5 if hh_b2_text_family_2==5
replace text_freq_2=0.25 if hh_b2_text_family_2==6
replace text_freq_2=0 if hh_b2_text_family_2==7 | hh_b2_text_family_2==8

rename hh_b3_talk_family_times_2 call_freq_2
rename hh_b4_length_conversation_mins_2 call_length_2

* Prepare treatment by employer
tab1 hh_c2_exp_shout_2 hh_c2_exp_violence_2 hh_c2_exp_threat_2 hh_c2_exp_sexharass_2
recode hh_c2_exp_shout_2 hh_c2_exp_violence_2 hh_c2_exp_threat_2 hh_c2_exp_sexharass_2 hh_c4_provide_food_2 (98 99 = .c)

recode hh_c2_exp_shout_2 (1=0) (0=1), gen(noshouting_2)
recode hh_c2_exp_violence_2 (1=0) (0=1), gen(noviolence_2)
recode hh_c2_exp_threat_2 (1=0) (0=1), gen(nothreat_2)
recode hh_c2_exp_sexharass_2 (1=0) (0=1), gen(nosexharass_2)

gen enoughfood_2 = hh_c4_provide_food_2

lab var noshouting_2 "Experienced no shouting"
lab var noviolence_2 "Experienced no violence"
lab var nothreat_2 "Experienced no threats"
lab var nosexharass_2 "Experienced no sexual harassment"
lab var enoughfood_2 "Have enough food"

* HSW work break from hh_c10_hours_break_textbox_2 
list rno hh_c10_hours_break_hrs_2 hh_c10_hours_break_textbox_2 if trim( hh_c10_hours_break_textbox_2)!=""
gen hh_c10_textbox_2=""
replace hh_c10_textbox_2=".25" if rno==132 // TNS coded it as 0.15
replace hh_c10_textbox_2=".33" if rno==218
replace hh_c10_textbox_2=".5" if rno==401 // 30 mins break
replace hh_c10_textbox_2=".17" if rno==584
replace hh_c10_textbox_2="1.25" if rno==690
replace hh_c10_textbox_2=".25" if rno==2121 // TNS coded it as 15 hours
replace hh_c10_textbox_2=".25" if rno==2587
replace hh_c10_textbox_2=".5" if rno==2649
replace hh_c10_textbox_2=".25" if rno==2654
replace hh_c10_textbox_2=".5" if rno==2717
replace hh_c10_textbox_2=".17" if rno==2742
destring hh_c10_textbox_2, replace
replace hh_c10_hours_break_hrs_2 = hh_c10_textbox_2 if hh_c10_textbox_2<.


* Calculate working hours

* start time
gen double starttime=clock( hh_c7_worktime_start_time_2,"DMYhms")
format %tc starttime
recode starttime (.=.c)

gen double starttime2=starttime
replace starttime2=starttime-msofhours(12) if hh(starttime)>=16 & hh(starttime)<=22 // subtract 12 if starttime is from 4 to 10 PM
format %tc starttime2
drop starttime
rename starttime2 work_start_2
recode work_start_2 (.=.c)

* end time
gen double endtime=clock(hh_c8_worktime_end_time_2,"DMYhms")
format %tc endtime
recode endtime (.=.c)

gen double endtime2=endtime
replace endtime2=endtime+msofhours(12) if hh(endtime)>5 & hh(endtime)<=12 //add 12 hrs if end time is after 5am and before 12nn
replace endtime2=endtime+msofhours(24) if hh(endtime)<=4 // add 24 hrs if before or equal to 5am
format %tc endtime2
drop endtime
rename endtime2 work_end_2
recode work_end_2 (.=.c)

* working hours (ANDREAS: December 3)
gen workhours_2=(work_end_2-work_start_2)/(60*60*1000)
gen worktime_2 = workhours_2 - hh_c10_hours_break_hrs_2 // break has many missings, so the number of observations decreases
replace worktime_2 = .c if worktime_2<0 | worktime_2>24
gen sparetime_2 = 24 - worktime_2

lab var workhours_2 "Gross working hours"
lab var worktime_2 "Net working hours"
lab var sparetime_2 "Non-working hours"

***********************************************************************


tab hh_c11_restday_2 
gen restday_2 = hh_c11_restday_2 == 1
replace restday_2 = .a if hh_c11_restday_2>=.a

tab hh_c12_leave_restday_2 
gen leavehouse_2 = 1 if hh_c12_leave_restday_2 == 1
replace leavehouse_2 = 0 if hh_c12_leave_restday_2 == 0
replace leavehouse_2 = 0 if restday_2 ==0 // if someone does not have a regular rest day I set this variable also to 0
replace leavehouse_2 = .c if hh_c12_leave_restday_2 == 98 | hh_c12_leave_restday_2 ==99
replace leavehouse_2 = .a if hh_c12_leave_restday_2 == .a
replace leavehouse_2 = .b if hh_c12_leave_restday_2 == .b

tab hh_d4_regular_salary_2
gen salary_ontime_2 = 1 if hh_d4_regular_salary_2 == 1
replace salary_ontime_2 = 0 if hh_d4_regular_salary_2 == 2 | hh_d4_regular_salary_2 == 3
replace salary_ontime_2 = .a if hh_d4_regular_salary_2 == .a
replace salary_ontime_2 = .b if hh_d4_regular_salary_2 == .b
replace salary_ontime_2 = .c if hh_d4_regular_salary_2 == .c

tab hh_d5_deduction_2
gen sal_noded_2 = hh_d5_deduction_2 ==3 if hh_d5_deduction_2<10

lab var restday_2 "HSW has rest day once a week"
lab var leavehouse_2 "HSW can leave the employer's house"
lab var salary_ontime_2 "HSW receives her salary on time"
lab var sal_noded_2 "HSW did not experience salary deduction"

* Employer described as good person
*tab hh_c1_description_employer_good_
* NOTE: THE VARIABLE NAMES ARE TO LONG AND CUT. THUS, RAW VARIABLES ARE NOT IDENTIFIED AS W2 OR W3
rename hh_c1_description_employer_good_ employer_good_2
rename hh_c1_description_employer_short employer_short_2 
rename hh_c1_description_employer_slave employer_slaver_2
rename hh_c1_description_employer_viole employer_violent_2
rename hh_c1_description_employer_stric employer_strict_2

gen noemployer_short_2 = 1 - employer_short_2
gen noemployer_slaver_2 = 1 - employer_slaver_2
gen noemployer_violent_2 = 1 - employer_violent_2
gen noemployer_strict_2 = 1 - employer_strict_2

lab val noemployer_*_2 employer_*_2 l_yesno

gen employer_subj_index_2 = employer_good_2 + noemployer_short_2 + noemployer_slaver_2 + noemployer_violent_2 + noemployer_strict_2

* Financial coordination
tab1 hh_d1_discuss_budget_2 hh_d2_discuss_remittance_amount_ hh_d3_discuss_remittance_use_2
recode hh_d1_discuss_budget_2 hh_d2_discuss_remittance_amount_ hh_d3_discuss_remittance_use_2 (.a .b = 3)
rename hh_d1_discuss_budget_2 dis_budget_2
rename hh_d2_discuss_remittance_amount_ dis_remit_amount_2
rename hh_d3_discuss_remittance_use_2 dis_remit_use_2

gen coord_success_2 = (dis_budget_2==1) + (dis_remit_amount_2 ==1) + (dis_remit_use_2==1)
gen coord_attempt_2 = coord_success + (dis_budget_2==2) + (dis_remit_amount_2==2) + (dis_remit_use_2==2)

replace coord_success_2 = .c if dis_budget_2==.c | dis_remit_amount_2==.c | dis_remit_use_2==.c
replace coord_attempt_2 = .c if dis_budget_2==.c | dis_remit_amount_2==.c | dis_remit_use_2==.c

lab var coord_attempt_2 "Financial coordination between HSW and family"
lab var coord_success_2 "Successful financial coordination between HSW and family"

tab hh_d13_extra_remittance_2
gen fam_extra_mon_2 = hh_d13_extra_remittance_2
lab var fam_extra_mon_2 "Family asked for extra money"

* Aware of gift treatment
gen remember_gift_2 = hh_c16_dried_mangoes_2
gen gave_gift_2=hh_c17_dried_mangoes_employer_2

lab var remember_gift_2 "Remember gift treatment"
lab var gave_gift_2 "Gave gift to the employer"

* Correct expectations
gen tasks_asexpected_2 = 1 if hh_c5_type_work_2 == 1
replace tasks_asexpected_2 = 0 if hh_c5_type_work_2 == 2 | hh_c5_type_work_2 == 3
gen hours_asexpected_2 = 1 if hh_c6_length_work_2 == 1
replace hours_asexpected_2 = 0 if hh_c6_length_work_2 == 2 | hh_c6_length_work_2 == 3
gen hours_longer_2 = 1 if hh_c6_length_work_2 == 3
replace hours_longer_2 = 0 if hh_c6_length_work_2 == 2 | hh_c6_length_work_2 == 1
gen hours_shorter_2 = 2 if hh_c6_length_work_2 == 2
replace hours_shorter_2 = 0 if hh_c6_length_work_2 == 1 | hh_c6_length_work_2 == 3
gen expectations_2 = hours_asexpected_2+tasks_asexpected_2

lab var hours_asexpected_2 "Working hours as expected"
lab var tasks_asexpected_2 "Tasks as expected"
lab var hours_longer_2 "Working hours longer than expected"
lab var hours_shorter_2 "Working hours shorter than expected"
lab var expectations_2 "Expectations index"

*replace . with .c
qui ds, has(type numeric)
local x=r(varlist)
recode `x' (.=.c) 

save "$data_prep/owwa_hh_w2_edited", replace













